﻿
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

using Carbon.Web.Forms.jQueryForms;

namespace Demo.Demos
{
    [Serializable]
    public class PollCulture
    {
        #region Properties (2)
        public int CultureId { get; set; }
        public String CultureName
        {
            get
            {
                return CultureInfo.GetCultureInfo(CultureId).EnglishName;
            }
        }
        #endregion
    }

    [Serializable]
    public class PollViewModel
    {
        #region Fields (1)
        private List<AnswerViewModel> m_Answers = new List<AnswerViewModel>();
        #endregion

        #region Properties (8)
        public DropDownListItemCollection<PollCulture, int?> AddCultureDropdown
        {
            get
            {
               CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

                HashSet<int> currentCultureIds = new HashSet<int>(Cultures.Select(i => i.CultureId));

                IEnumerable<PollCulture> culturesWithoutCurrent = cultures.Where(i => !currentCultureIds.Contains(i.LCID)).Select(i => new PollCulture() { CultureId = i.LCID });
               
                return new DropDownListItemCollection<PollCulture, int?>(culturesWithoutCurrent, (i) => i.CultureId, i => i.CultureName, null, true);
            }
            set
            {
                if (value != null && value.SelectedKey != null)
                {
                    Cultures.Add(value.SelectedItem);
                }
            }
        }
        public List<AnswerViewModel> Answers
        {
            get
            {
                return m_Answers;
            }
        }
        public bool CanAnonymousVote { get; set; }
        public List<PollCulture> Cultures { get; set; }
        public DateTime PublishDate { get; set; }
        public String Question { get; set; }
        public String SummaryText { get; set; }
        public String Title { get; set; }
        #endregion

        #region Constructors (1)
        public PollViewModel()
        {
            Cultures = new List<PollCulture>() { new PollCulture() { CultureId = 1033 }, new PollCulture() { CultureId = 1050 } };

            this.Answers.Add(new AnswerViewModel(this));
            this.Answers.Add(new AnswerViewModel(this));
        }
        #endregion

        #region Methods (4)

        public void AddNewAnswer()
        {
            AnswerViewModel awm = new AnswerViewModel(this);
            this.Answers.Add(awm);
        }

        public void RemoveAnswer(AnswerViewModel answerViewModel)
        {
            this.Answers.Remove(answerViewModel);
        }

        public void ValidateAnswers()
        {
            if (Answers.Count < 2)
                throw new ApplicationException("At least two answers must be entered.");
        }

        #endregion
    }
}